package com.demo.cms.action.manage;

import com.demo.common.SystemTools;
import com.demo.common.SystemTools.SystemInfo;
import com.demo.common.monitor.MonitorInfo;
import com.demo.common.monitor.MonitorService;
import com.demo.common.monitor.impl.MemoryMonitor;
import com.demo.core.model.Member;
import com.demo.core.model.User;
import com.demo.core.service.base.MemberService;
import com.demo.core.service.base.UserService;
import com.demo.core.service.exception.UserNotFoundOrPasswordErrorException;
import com.demo.security.annotation.function.Function;
import com.demo.security.util.SecurityUtil;
import com.demo.webapp.action.PageAction;

/**
 * 系统首页部分Action
 * @author ryuu.kk
 *
 */
@Function(id = 100400, pid = 10, code = "manage.home")
public class HomeAction extends PageAction {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -6962530336268879872L;
	
	/**
	 * 伊甸园最大使用内存
	 */
	private float edenMax;
	
	/**
	 * 最大内存使用
	 */
	private float[] memMax;

	/**
	 * 指标名称
	 */
	private String[] ptName;
	/**
	 * 系统信息
	 */
	private SystemInfo sysInfo;
	
	/**
	 * 监控信息
	 */
	private MonitorInfo monitorInfo;
	
	/**
	 * 用户模型
	 */
	private User user;
	
	/**
	 * 会员模型
	 */
	private Member member;
	
	/**
	 * 监控服务
	 */
	private MonitorService monitorService;
	
	/**
	 * 用户服务
	 */
	private UserService userService;
	
	/**
	 * 会员服务
	 */
	private MemberService memberService;
	
	/**
	 * 旧密码
	 */
	private String oldpass;
	/**
	 * 新密码
	 */
	private String newpass;
	/**
	 * 重置密码
	 */
	private String repass;
	
	/**
	 * 欢迎页
	 * @return String
	 */
	@Function(id = 100401, pid = 100400, code = "manage.home.welcome")
	public String welcome() {
		
		sysInfo = SystemTools.getSystemInfo();
		monitorInfo = SystemTools.getSystemMonitor();
		memMax = monitorService.plotMemoryMax();
		// 伊甸园最大内存使用
		edenMax = memMax[1] / 1024;
		ptName = monitorService.plotMemoryName();
		return SUCCESS;
	}
	
	/**
	 * 取得当前内存使用情况
	 * @return String
	 */
	public String usedMem() {
		MemoryMonitor mm = new MemoryMonitor();
		mm.paint();
		
		return SUCCESS;
	}
	
	/**
	 * 个人信息管理
	 * @return String
	 */
	@Function(id = 100402, pid = 100400, code = "manage.home.owner")
	public String owner() {
		Long siteId = getSite().getId();
		Long userId = SecurityUtil.getCurrentUserId();
		// 提高用户信息的准确性,从DB中读取
		user = userService.find(userId);
		member = memberService.find(userId, siteId);

		return SUCCESS;
	}
	
	/**
	 * 个人账户设置
	 * @return String
	 */
	@Function(id = 100403, pid = 100400, code = "manage.home.accset")
	public String accset() {
		
		//Long siteId = getSite().getId();
		//Long userId = SecurityUtil.getCurrentUserId();
		// 提高用户信息的准确性,从DB中读取
		//user = userService.find(userId);
		
		return SUCCESS;
	}
	/**
	 * 个人信息更新
	 * @return String
	 */
	@Function(id = 100404, pid = 100400, code = "manage.home.upinfo")
	public String upinfo() {
		
		return CALLBACK;
	}
	
	/**
	 * 更新登录密码
	 * @return String
	 */
	@Function(id = 100405, pid = 100400, code = "manage.home.uppass")
	public String uppass() {

		//com.opensymphony.xwork2.validator.validators.RequiredStringValidator
		//Long siteId = getSite().getId();
		Long userId = SecurityUtil.getCurrentUserId();
		// 提高用户信息的准确性,从DB中读取
		//user = userService.find(userId);
		User user = userService.find(userId);
		
		if (user != null) {
			// 旧密码确认,更新新密码
			try {
				// 重置密码
				userService.resetPassword(userId, oldpass, newpass);
				addActionMessage(getText("user.newpassword.reset.success"));
			} catch (UserNotFoundOrPasswordErrorException e) {
				addFieldError("oldpass", getText("user.oldpassword.error"));
			}
		} else {
			addActionError(getText("user.notfound.error"));
		}
		return SUCCESS;
	}
	
	public SystemInfo getSysInfo() {
		return sysInfo;
	}

	public MonitorInfo getMonitorInfo() {
		return monitorInfo;
	}

	public float getEdenMax() {
		return edenMax;
	}

	public float[] getMemMax() {
		return memMax;
	}

	public String[] getPtName() {
		return ptName;
	}

	public void setOldpass(String oldpass) {
		this.oldpass = oldpass;
	}

	public void setNewpass(String newpass) {
		this.newpass = newpass;
	}

	public String getOldpass() {
		return oldpass;
	}

	public String getNewpass() {
		return newpass;
	}

	public String getRepass() {
		return repass;
	}

	public void setRepass(String repass) {
		this.repass = repass;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void setMonitorService(MonitorService monitorService) {
		this.monitorService = monitorService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
}
